package com.i2bt.app.login;

import com.i2bt.common.AndroidRestContract;

import com.i2bt.R;
import com.i2bt.app.BaseActivity;
import com.i2bt.app.login.model.RegistJsonHandle;
import com.i2bt.bean.RegistBean;

import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

public class RegistActivty extends BaseActivity implements TextWatcher {
	Button regist;
	EditText userId, password, enterPassword, phone;
	CheckBox checkBox;
	TextView userProtocol, warnToast;
	String userIdStr, passwordStr, enterPasswordStr, phoneStr = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		if (Build.VERSION.SDK_INT < 11) {
			actionBarSupport.hide();
		}
		setContentView(R.layout.activity_regist);
		initView();
	}

	/**
	 * 点击空白处隐藏输入法
	 */
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub
		InputMethodManager manager;
		manager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
		if (event.getAction() == MotionEvent.ACTION_DOWN) {
			if (getCurrentFocus() != null
					&& getCurrentFocus().getWindowToken() != null) {
				manager.hideSoftInputFromWindow(getCurrentFocus()
						.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
			}
		}
		return super.onTouchEvent(event);
	}

	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		if (arg0 == regist) {
			warnToast.setVisibility(View.INVISIBLE);
			userIdStr = userId.getText().toString();
			passwordStr = password.getText().toString();
			enterPasswordStr = enterPassword.getText().toString();
			phoneStr = phone.getText().toString();
			logic();
		} else if (arg0 == userProtocol) {
			toastHandler("尚未存在！");
		}
	}

	@Override
	protected void initView() {
		// TODO Auto-generated method stub
		regist = (Button) this.findViewById(R.id.regist_button);
		userId = (EditText) this.findViewById(R.id.regist_UserID);
		password = (EditText) this.findViewById(R.id.regist_password);
		enterPassword = (EditText) this
				.findViewById(R.id.regist_makesure_password);
		phone = (EditText) this.findViewById(R.id.regist_phone);
		checkBox = (CheckBox) this.findViewById(R.id.regist_checkBox);
		userProtocol = (TextView) this.findViewById(R.id.regist_userProtocol);
		warnToast = (TextView) this.findViewById(R.id.regist_Toast);
		regist.setOnClickListener(this);
		userProtocol.setOnClickListener(this);
		userId.addTextChangedListener(this);
		password.addTextChangedListener(this);
		enterPassword.addTextChangedListener(this);
	}

	@Override
	protected void logic() {
		// TODO Auto-generated method stub
		if (userIdStr.length() == 0) {
			showWarnToast("请输入用户名");
		} else if (passwordStr.length() == 0) {
			showWarnToast("密码不能为空");
		} else if (passwordStr.length() < 6) {
			showWarnToast("密码长度不足，需要六位密码");
		} else if (enterPasswordStr.length() == 0) {
			showWarnToast("请确认密码");
		} else if (!passwordStr.equals(enterPasswordStr)) {
			showWarnToast("密码不一致");
		} else if (phoneStr.length() != 0 && phoneStr.length() != 11) {
			showWarnToast("手机号码长度错误");
		} else if (!checkBox.isChecked()) {
			showWarnToast("请同意用户协议");
		} else {
			RegistBean bean = new RegistBean();
			bean.setAct(AndroidRestContract.REGIST_INFO);
			bean.setUserId(userIdStr);
			bean.setNickName(userIdStr);
			bean.setPassword(passwordStr);
			bean.seteMail(null);
			bean.setPhone(phoneStr);
			new RegistJsonHandle(this).postTo(AndroidRestContract.REST_REGIST,
					bean);
		}
	}

	/**
	 * 显示警告信息
	 * 
	 * @param Str显示的信息
	 */
	public void showWarnToast(String Str) {
		warnToast.setText(Str);
		warnToast.setVisibility(View.VISIBLE);
	}

	@Override
	public void afterTextChanged(Editable arg0) {
		// TODO Auto-generated method stub
		if (arg0.length() > 0) {
			int pos = arg0.length() - 1;
			char c = arg0.charAt(pos);
			if (c == ' ' || c == '.') {
				arg0.delete(pos, pos + 1);
				toastHandler("输入违法!");
			}
		}
	}

	@Override
	public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
			int arg3) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
		// TODO Auto-generated method stub
		if (warnToast.getVisibility() == View.VISIBLE) {
			if ((!password.getText().toString().equals(""))
					&& (!userId.getText().toString().equals(""))
					&& (!enterPassword.getText().toString().equals(""))) {
				warnToast.setVisibility(View.INVISIBLE);
			}
		}
	}

	@Override
	public void backHomeOperate() {
		// TODO Auto-generated method stub
		
	}

}
